﻿using System.Collections.Generic;

namespace SnippetBox.Core.Data.Models {
    /// <summary>
    /// Used to parse a string to a Role enum. 
    /// The .NET way of doing it: (T)Enum.Parse(typeof(T), value, true) is way too slow!
    /// </summary>
    /// <remarks>
    /// Is this a micro-optimization? Yes.
    /// </remarks>
    public static class RoleParser {
        private static readonly Dictionary<string, Role> _roles = new Dictionary<string, Role>
        {
            {Roles.MEMBER, Role.Member},
            {Roles.EDITOR, Role.Editor},
            {Roles.ADMIN, Role.Admin},
            {Roles.SUPER_ADMIN, Role.SuperAdmin},
        };

        public static Role Parse(string role) {
            return _roles.ContainsKey(role) ? _roles[role] : Role.Member;
        }
    }
}